package lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : wenchao.long
 * @date : Created in 2024-03-22 下午 1:30 周五
 * @description :
 */
public class ReEnterLockDemo {

	static final Object objectLock = new Object();

	static Lock lock = new ReentrantLock();

	static void m1() {
		new Thread(() -> {
			synchronized (objectLock) {
				System.out.println(Thread.currentThread().getName() + "\t" + "----外层调用");
				synchronized (objectLock) {
					System.out.println(Thread.currentThread().getName() + "\t" + "----中层层调用");
					synchronized (objectLock) {
						System.out.println(Thread.currentThread().getName() + "\t" + "----内层调用");
					}
				}
			}
		}, "t1").start();
	}

	public synchronized void m2() {
		System.out.println("m2");
		m3();
	}

	public synchronized void m3() {
		System.out.println("m3");
		m4();
	}

	public synchronized void m4() {
		System.out.println("m4");
	}

	public static void main(String[] args) {
		new Thread(() -> {
			lock.lock();
			try {
				System.out.println("---外层");
				lock.lock();
				try {
					System.out.println("---内层");
				} finally {
					lock.unlock();
				}
			} finally {
				lock.unlock();
			}
		}, "t2").start();

		new Thread(() -> {
			lock.lock();
			try {
				System.out.println(Thread.currentThread().getName() + "\t" + "---调用开始");
			} finally {
				lock.unlock();
			}
		}, "t3").start();
	}
}
